package com.hnu.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnu.seckill.entity.TUser;
import com.hnu.seckill.exception.GlobalException;
import com.hnu.seckill.mapper.UserMapper;
import com.hnu.seckill.service.IUserService;
import com.hnu.seckill.utils.CookieUtil;
import com.hnu.seckill.utils.MD5Util;
import com.hnu.seckill.utils.UUIDUtil;
import com.hnu.seckill.utils.ValidatorUtil;
import com.hnu.seckill.vo.LoginVo;
import com.hnu.seckill.vo.RespBean;
import com.hnu.seckill.vo.RespBeanEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2025-04-16
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, TUser> implements IUserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    RedisTemplate redisTemplate;



    @Override
    public RespBean doLogin(@Valid LoginVo loginVo, HttpServletResponse response, HttpServletRequest request) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        if(mobile == null || mobile.isEmpty() || password == null || password.isEmpty()) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if(!ValidatorUtil.isMobile(mobile)) {
            log.info("手机号码格式错误");
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }
        TUser TUser = userMapper.selectById(mobile);
        if(TUser == null) {
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        if(!MD5Util.formPassToDBPass(password, TUser.getSalt()).equals(TUser.getPassword())){
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        String ticket = UUIDUtil.uuid();
//        request.getSession().setAttribute(ticket,user);
        redisTemplate.opsForValue().set("user:"+ticket, TUser);
        CookieUtil.setCookie(request,response,"userTicket",ticket);


        return RespBean.success(ticket);

    }

    @Override
    public TUser getUserByCookie(String ticket, HttpServletRequest request, HttpServletResponse response) {
        if(ticket == null || ticket.isEmpty()) {
            return null;
        }
        TUser TUser = (TUser) redisTemplate.opsForValue().get("user:" + ticket);

        if(TUser !=null){
            CookieUtil.setCookie(request,response,"userTicket",ticket);

        }

        return TUser;
    }

    @Override
    public RespBean updatePassword(String userTicket, String newPassword, HttpServletRequest request, HttpServletResponse response) {
        TUser TUser = getUserByCookie(userTicket, request, response);
        if(TUser == null) {
            return RespBean.error(RespBeanEnum.PHNOE_NOT_EXIST);
        }
        TUser.setPassword(MD5Util.inputPassToDBPass(newPassword, TUser.getSalt()));
        int res= userMapper.updateById(TUser);
        if(res ==1){
            redisTemplate.delete("user:"+userTicket);
            redisTemplate.opsForValue().set("user:"+userTicket,TUser);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.UPDATE_FAIL);

    }


}
